/**
 * Created by zhang_renyang on 15/1/3.
 */

var dateUtils = require('../utils/dateUtils');
var categoryModel = require('../schema/category');
var articleModel = require('../schema/article');

function Category(category) {
    this.name = category.name;
    this.parent_id = category.parent_id;
    this.order_by = category.order_by;
};

/**
 * 保存栏目
 * @param callback
 */
Category.prototype.save = function(callback) {
    //要存入数据库的用户信息文档
    var category = {
        name: this.name,
        order_by:this.order_by,
        create_time:dateUtils.getTime(),
        update_time:dateUtils.getTime()
    };
    if(this.parent_id)
        category.parent_id = this.parent_id;
    else
        category.type = 0;
    var newCategory = new categoryModel(category);
    newCategory.save(function(err,category){
        console.error(err);
        if(err)
            return callback(err);
        callback(null,category);
    });
};

/**
 * 保存栏目
 * @param callback
 */
Category.prototype.update = function(_id,callback) {
    //要存入数据库的用户信息文档
    var category = {
        name: this.name,
        order_by: this.order_by,
        update_time:dateUtils.getTime()
    };
    if(this.parent_id){
        category.type = 1;
        category.parent_id = this.parent_id;
    }else{
        category.type = 0;
    }
    var opObject = {"$set":category};
    if(!this.parent_id){
        opObject["$unset"]={"parent_id":1};
    }
    categoryModel.update({_id:_id},opObject,function(err,category){
        if(err)
            return callback(err);
        callback(null,category);
    });

};

//根据ID查询栏目
Category.findById = function(_id,callback){
    categoryModel.findOne({_id:_id},null,{}).populate('parent_id').exec(function(err,category){
        if(err){
            return callback(err);
        } else{
            callback(null,category);
        }
    });

}

Category.deleteById = function(_id,callback){
    categoryModel.remove({_id:_id}, function(err){
        callback(err);
    });
}

Category.tree = function(callback){
    categoryModel.find({},null,{}).sort({order_by:-1}).populate('parent_id').exec(function(err,categories){
        if(err){
            callback(err);
        }else{
            articleModel.find({},null,{}).sort({order_by:-1}).populate('user_id').populate('category_id').exec(function(err,articles){
                if(err){
                    callback(err);
                }else {
                    if (categories && categories.length) {
                        for (var i = 0; i < categories.length; i++) {
                            var category = categories[i];
                            for (var j = 0; j < categories.length; j++) {
                                if (categories[j].parent_id && categories[j].parent_id._id.toString() == category._id.toString()) {
                                    if (!(category.children)) {
                                        category.children = [];
                                    }
                                    category.children.push(categories[j]);
                                }
                            }
                        }
                        if (articles && articles.length) {
                            for (var i = 0; i < categories.length; i++) {
                                var category = categories[i];
                                for (var j = 0; j < articles.length; j++) {
console.log((articles[j].category_id?articles[j].category_id._id.toString():'')+" "+category._id.toString()+(articles[j].category_id?articles[j].category_id._id.toString():'' == category._id.toString()));
                                    if (articles[j].category_id && articles[j].category_id._id.toString() == category._id.toString()) {
                                        if (!(category.articles)) {
                                            category.articles = [];
                                        }
                                        category.articles.push(articles[j]);
                                    }
                                }
                            }
                        }
                        var result_categories = [];
                        if (categories && categories.length) {
                            for (var i = 0; i < categories.length; i++) {
                                if (!(categories[i].parent_id))
                                    result_categories.push(categories[i]);
                            }
                        }
                }
                    callback(err, result_categories);
                }
            });
        }
    });
}

Category.pageQuery = function(query,callback){
    categoryModel.count({},function(err,count){
        categoryModel.find({},null,{}).sort({order_by:-1}).skip((query.pageNum-1)*query.pageSize).limit(query.pageSize).populate('parent_id').exec(function(err,categories){
            if(err){
                callback(err);
            }else{
                callback(null,count,categories);
            }
        });
    });
}



module.exports = Category;

